package edu.westga.sketchit2009cs.gameEngine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Takes in a string and creates a list of the words
 * separating them by either the default delimiter ":"
 * or by a specified delimiter.
 * 
 * Also has the ability to get a word from a specified index,
 * get the current word, get the next word, and check
 * if the next word exists.
 * 
 * @author RJ Hill
 *
 */
public class WordListHandler {
	
	private List<String> wordList;
	private int i;
		
	/**
	 * WordListHandler initializer
	 */
	public WordListHandler() {
		
	}
	
	/**
	 * createWordList takes a string and creates a list of words
	 * using the default delimiter ":".
	 * <p>
	 * Requires: wordListString != null
	 * <p>
	 * Ensures: wordList(i) == this.getword(i)
	 * @param wordListString the delimited string containing the words
	 */
	public void createWordList(String wordListString) {
		String defaultDelimiter = ":";
		createWordList(wordListString, defaultDelimiter);
	}
	
	/**
	 * createWordList takes a string and creates a list of words
	 * using a specified delimiter.
	 * <p>
	 * Requires: wordListString != null && delimiter != null
	 * <p>
	 * Ensures: 
	 * @param wordListString the delimited string containing the words
	 * @param delimiter the specific delimiter used in the string
	 */
	public void createWordList(String wordListString, String delimiter) {
		wordList = new ArrayList<String>();
		wordList = Arrays.asList(wordListString.split(delimiter));
	}
	
	/**
	 * Gets the first word in the list and assigns as the category
	 * then removes it from the list.
	 * <p>
	 * Requires: wordList != null
	 * <p>
	 * Ensures: category == this.getCurrentWord()@pre
	 * @return the category
	 */
	public String getCategory() {
		String category = this.wordList.get(0);
		this.nextWord();
		
		return category;
	}
	
	/**
	 * Gets a word from a specific index in the list.
	 * <p>
	 * Requires: i <= 0 && wordList != null
	 * <p>
	 * Ensures: word = this.wordList.get(i)
	 * @param i the index number
	 * @return the word at the specific index number
	 */
	public String getWord(int i) {
		String word = this.wordList.get(i);
		
		return word;
	}
	
	/**
	 * Gets the first word in the list.
	 * <p>
	 * Requires: wordList != null
	 * <p>
	 * Ensures: nothing.
	 * @return the first word in the list
	 */
	public String getCurrentWord() {
		return this.getWord(i);
	}
	
	/**
	 * Moves on to the next word in the list.
	 * @return
	 */
	public String nextWord() {
		if(this.nextWordExists()) {
			i++;
			return this.getWord(i);
		}
		else {
			return null;
		}
	}
	
	/**
	 * Checks to see if the next word exists.
	 * @return
	 */
	public boolean nextWordExists() {
		if(i < this.wordList.size()) {
			return true;
		}
		else {
			return false;
		}
	}
	
}
